﻿using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using Dcms.BLL;
using Dcms.Common;
using Dcms.Model;
using WebApp.AppCode;


public partial class Manage_Modle_News_ListNews : System.Web.UI.Page
{
    public CompetenceLib CL = new CompetenceLib();
    Dcms_NewsBLL nBll = new Dcms_NewsBLL();
    Dcms_CateBLL dcb = new Dcms_CateBLL();
    Dcms_CateModel dcm;

    protected void Page_Load(object sender, EventArgs e)
    {
        CL.IsLogin();
        try
        {
            if (CL.IsDelete(Convert.ToInt32(strCateID)) == false)
            {
                btnDelete.Visible = false;
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_News_ListNews.Page_Load", ex);
            throw ex;
        }	
    }

    private string GetWhere()
    {
        string strWhere = string.Format(" where News_CateId={0} ", strCateID);
        if (this.txtNewsID.Text.Trim() != "")
        {
            strWhere += string.Format(" and News_Id={0} ", this.txtNewsID.Text.Trim()); 
        }
        if (this.txtNewsTitle.Text.Trim() != "")
        {
            strWhere += string.Format(" and News_Title like '%{0}%' ", this.txtNewsTitle.Text.Trim());
        }
        return strWhere;
    }

    private void BindData()
    {
        string strWhere = GetWhere();
        this.hidAmount.Value = nBll.GetDataCount(string.Format("select count(*) from Dcms_News {0}", strWhere).ToString());
        this.hidSql.Value = string.Format("select * from Dcms_News {0} Order by News_Id desc", strWhere);
        ReBinder(1);



        ///移动复制下拉框
        dcm = dcb.GetModelByID(Convert.ToInt32(strCateID));
        string where = " Cate_Flag='" + dcm.Cate_Flag + "' and Cate_ModelKeyId=" + dcm.Cate_ModelKeyId + " and Cate_showtype=1 and  Cate_Lang='" + dcm.Cate_Lang + "' and Cate_ID<>" + dcm.Cate_ID;
        dropType.DataTextField = "Cate_Name";
        dropType.DataValueField = "Cate_ID";
        dropType.DataSource = dcb.GetList(where);
        //dropType.Items.Insert(0,new ListItem(dcm.Cate_Name, dcm.Cate_ID.ToString()));
        dropType.DataBind();
       
        
    }

    protected void BindRpt(object sender, PagingEventArgs e)
    {
        this.ReBinder(e.pageIndex);
    }

    private void ReBinder(int PageIndex)
    {
        this.repIntor.DataSource = nBll.GetDataListByPage(this.hidSql.Value, PageIndex, 10);
        this.repIntor.DataBind();
        pagingctrl.InitPc(Convert.ToInt32(this.hidAmount.Value), PageIndex, 10);
    }

    public String GetStrLeng(String Str)
    {
        return Utils.GetSubString(Str, 30, "...");
    }
    public String FormatDateTimeMy(String Str)
    {
        return Convert.ToDateTime(Str).ToString("yyyy-MM-dd");
    }

    protected void repnew_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            LinkButton HypDelete = (LinkButton)e.Item.FindControl("HypDelete");
            if (CL.IsDelete(Convert.ToInt32(strCateID)))
            {
                HypDelete.Attributes.Add("onclick", "return confirm('确定要删除该记录吗？');");
            }
            else
            {
                HypDelete.Text = "<img src=\"../../images/ico_del_1.gif\" width=\"16\" height=\"16\" alt=\"您无权限使用删除\" />";
                HypDelete.Enabled = false;
            }
            LinkButton btnEdit = (LinkButton)e.Item.FindControl("btnEdit");
            if (CL.IsEdit(Convert.ToInt32(strCateID)) == false)
            {
                btnEdit.Text = "<img src=\"../../images/edit_1.gif\" width=\"16\" height=\"16\" alt=\"您无权限使用编辑\"/>";
            }
        }
    }

    protected void repnew_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (e.CommandName == "delete")
            {
                int Cate_ID = DcmsRequest.GetQueryInt("CatID", 0);
                nBll.DeleteModelByID(Convert.ToInt32(((HiddenField)e.Item.FindControl("hidID")).Value));
                Response.Redirect(Request.Url.ToString());
            }
            else if(e.CommandName == "edit")
            {
                Response.Redirect(((HiddenField)e.Item.FindControl("hidEditUrl")).Value);
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int Cate_ID = DcmsRequest.GetQueryInt("CatID", 0);
        string AllID = DcmsRequest.GetFormString("ID");
        if (AllID.Trim().Length > 0)
        {
            nBll.DeleteAllCheck(AllID);
            Response.Redirect(Request.Url.ToString()); 
        }
        else
        {
            Jscript.AlertAndBack("没有选中项");
        }
    }
    protected void btnPass_Click(object sender, EventArgs e)
    {
        int Cate_ID = DcmsRequest.GetQueryInt("CatID", 0);
        string AllID = DcmsRequest.GetFormString("ID");
        if (AllID.Trim().Length > 0)
        {
            nBll.UpdateState(AllID, 1);
            Response.Redirect(Request.Url.ToString()); 
        }
        else
        {
            Jscript.AlertAndBack("没有选中项");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        int Cate_ID = DcmsRequest.GetQueryInt("CatID", 0);
        string AllID = DcmsRequest.GetFormString("ID");
        if (AllID.Trim().Length > 0)
        {
            nBll.UpdateState(AllID, 0);
            Response.Redirect(Request.Url.ToString()); 
        }
        else
        {
            Jscript.AlertAndBack("没有选中项");
        }
    }

    /// <summary>
    /// 复制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCopy_Click(object sender, EventArgs e)
    {

     
        string AllID = DcmsRequest.GetFormString("ID");
        if (AllID.Trim().Length > 0)
        {
            if (dropType.SelectedValue != strCateID)
            {
                if (AllID.Trim().Split(',').Length > 0)
                {
                    foreach (string s in AllID.Trim().Split(','))
                    {
                        Dcms_NewsBLL dnb = new Dcms_NewsBLL();
                        Dcms_NewsModel dnm = dnb.GetModelByID(Convert.ToInt32(s));
                        dnm.News_CateId = Convert.ToInt32(dropType.SelectedValue);
                        dnb.AddModel(dnm);
                       
                    }
                   
                    Response.Redirect(Request.Url.ToString()); 
                }
            }
            else
            {
                Jscript.AlertAndBack("不能复制");
            }
        }
        else
        {
            Jscript.AlertAndBack("没有选中项");
        }
    }

    /// <summary>
    /// 移动
    /// </summary>
    protected void btnMove_Click(object sender, EventArgs e)
    {
    
        string AllID = DcmsRequest.GetFormString("ID");
        if (AllID.Trim().Length > 0)
        {
            if (dropType.SelectedValue != strCateID)
            {
                if (AllID.Trim().Split(',').Length > 0)
                {
                    foreach (string s in AllID.Trim().Split(','))
                    {
                        Dcms_NewsBLL dnb = new Dcms_NewsBLL();
                        Dcms_NewsModel dnm = dnb.GetModelByID(Convert.ToInt32(s));
                        dnm.News_CateId = Convert.ToInt32(dropType.SelectedValue);
                        dnb.Update(dnm);
                        
                    }
                   
                    Response.Redirect(Request.Url.ToString()); 
                }
            }
            else
            {
                Jscript.AlertAndBack("不能移动");
            }
        }
        else
        {
            Jscript.AlertAndBack("没有选中项");
        }
    }

    public string strCateID
    {
        get
        {
            return Request.QueryString["CatID"] + "";
        }
    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
